Ext.define('UX.codeview.ComplexFieldX', {
	extend : 'Ext.form.FieldContainer',
	alias : 'widget.codeviewcomplexfieldx',

	requires : [ 'UX.codeview.ButtonX' ],

	layout : {
		type : 'hbox'
	},

	childItmIds : {},
	childNames : {},
	childAllowBlanks : {},
	childSubmitValues : {},
	childReadOnlys : {},
	childHiddens : {},
	
	initComponent : function() {
		
		this.txtItemId1 = !Ext.isEmpty(this.childItmIds['field1']) ? this.childItmIds['field1'] : Ext.id();
		this.txtItemId2 = !Ext.isEmpty(this.childItmIds['field2']) ? this.childItmIds['field2'] : Ext.id();
		this.codeviewItemId = Ext.id();

		var textfield1 = {
			xtype : 'textfield',
			itemId : this.txtItemId1,
			readOnly : true,
			flex : 1
		}

		var textfield2 = {
			xtype : 'textfield',
			itemId : this.txtItemId2,
			readOnly : true,
			submitValue : false,
			flex : 1
		}

		if(this.childHiddens['field2'] !== true){
			textfield1.margin = '0 5 0 0';
		}

		var textfields = {
			textfield1 : textfield1,
			textfield2 : textfield2
		}

		for (var i = 1; i <= 2; i++) {
			var field = 'field' + i;
			var textfield = textfields['textfield' + i];
			
			if (!Ext.isEmpty(this.childNames[field])) textfield.name = this.childNames[field];
			if (!Ext.isEmpty(this.childAllowBlanks[field])) textfield.allowBlank = this.childAllowBlanks[field];
			if (!Ext.isEmpty(this.childSubmitValues[field])) textfield.submitValue = this.childSubmitValues[field];
			if (!Ext.isEmpty(this.childReadOnlys[field])) textfield.readOnly = this.childReadOnlys[field];
			if (!Ext.isEmpty(this.childHiddens[field])) textfield.hidden = this.childHiddens[field];
		}

		var codeviewbutton = this.setCodeviewButton();

		this.items = [ textfield1, textfield2, codeviewbutton ];

		this.callParent(arguments);

		var self = this;
		
		var btnCodeview = this.getSubItem(this.codeviewItemId);
		btnCodeview.on('select', function(btn, records) {
			self.fireEvent('select', btn, records);
		});

		btnCodeview.on('beforestoreload', function(btn, store, params, selected) {
			self.fireEvent('beforestoreload', btn, store, params, selected);
		});

		btnCodeview.on('afterstoreload', function(btn, store, records, success) {
			self.fireEvent('afterstoreload', btn, store, records, success);
		});
		
		btnCodeview.on('clearfieldvalue', function(btn) {
			self.fireEvent('clearfieldvalue', btn);
		});
	},

	setCodeviewButton : function() {
		var codeviewbutton = {
			xtype : 'codeviewbuttonx',
			itemId : this.codeviewItemId
		}

		if (!Ext.isEmpty(this.type)) codeviewbutton.type = this.type;
		if (!Ext.isEmpty(this.title)){
			codeviewbutton.title = this.title;
		}else{
			if(!Ext.isEmpty(this.fieldLabel)){
				codeviewbutton.title = this.fieldLabel;
			}
		}
		if (!Ext.isEmpty(this.fieldSearch)) codeviewbutton.fieldSearch = this.fieldSearch;
		if (!Ext.isEmpty(this.remoteFilter)) codeviewbutton.remoteFilter = this.remoteFilter;
		if (!Ext.isEmpty(this.disabledEnter)) codeviewbutton.disabledEnter = this.disabledEnter;
		if (!Ext.isEmpty(this.factoryConditionEnabled)) codeviewbutton.factoryConditionEnabled = this.factoryConditionEnabled;
		if (!Ext.isEmpty(this.service)) codeviewbutton.service = this.service;
		codeviewbutton.columns = Ext.isEmpty(this.columns) ? [ {
			text : 'Name',
			dataIndex : 'key1',
			disabledSearch : false,
			disabledEnter : false,
			fieldId : this.txtItemId1,
			flex : 1
		}, {
			text : 'Description',
			dataIndex : 'data1',
			disabledSearch : false,
			disabledEnter : false,
			fieldId : this.txtItemId2,
			flex : 2
		} ] : Ext.clone(this.columns);
		if (!Ext.isEmpty(this.params)) codeviewbutton.params = this.params;
		if (!Ext.isEmpty(this.table)) codeviewbutton.table = this.table;
		if (!Ext.isEmpty(this.factory)) codeviewbutton.factory = this.factory;
		if (!Ext.isEmpty(this.order)) codeviewbutton.order = this.order;
		if (!Ext.isEmpty(this.condition)) codeviewbutton.condition = this.condition;
		if (!Ext.isEmpty(this.pageSize)) codeviewbutton.pageSize = this.pageSize;
		if (!Ext.isEmpty(this.multiSelect)) codeviewbutton.multiSelect = this.multiSelect;
		if (!Ext.isEmpty(this.multiSelectCount)) codeviewbutton.multiSelectCount = this.multiSelectCount;
		if (!Ext.isEmpty(this.delimiter)) codeviewbutton.delimiter = this.delimiter;
		if (!Ext.isEmpty(this.defaultWidthDisabled)) codeviewbutton.defaultWidthDisabled = this.defaultWidthDisabled;
		
		return codeviewbutton;
	},

	setValue : function(value, fieldNumber){
		var txtfield1 = this.getSubItem(this.txtItemId1);
		var txtfield2 = this.getSubItem(this.txtItemId2);
		
		if(Ext.isEmpty(fieldNumber) || fieldNumber === 1){
			return txtfield1.setValue(value);
		}else{
			return txtfield2.setValue(value);
		}
	},
	
	getValue : function(fieldNumber){
		var txtfield1 = this.getSubItem(this.txtItemId1);
		var txtfield2 = this.getSubItem(this.txtItemId2);
		
		if(Ext.isEmpty(fieldNumber) || fieldNumber === 1){
			return txtfield1.getValue();
		}else{
			return txtfield2.getValue();
		}
	},
	
	getValues : function(){
		var txtfield1 = this.getSubItem(this.txtItemId1);
		var txtfield2 = this.getSubItem(this.txtItemId2);
		
		return {
			field1 : txtfield1.getValue(),
			field2 : txtfield2.getValue()
		};
	},
	
	setValues : function(value1, value2){
		var txtfield1 = this.getSubItem(this.txtItemId1);
		var txtfield2 = this.getSubItem(this.txtItemId2);
		
		txtfield1.setValue(value1);
		txtfield2.setValue(value2);
	},
	
	getCodeview : function(){
		return this.getSubItem(this.codeviewItemId);
	},

	getSubItem : function(itemId) {
		return this.down('#' + itemId);
	}
});